<?php
/**
 * 推荐模块
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\UserService;


use Illuminate\Http\Request;
use App\Services\Tool\GeneralTool;
use DB;
use App\Services\UserService\UserTool;
use App\Services\ProductsService\ProductTool;

class CommendService
{

    protected $UserTool;
    protected $Product;

    public function __construct(UserTool $UserTool, ProductTool $Product)
    {
        $this->UserTool = $UserTool;
        $this->Product = $Product;
    }


    /**
     *
     * 现为所有设计师与品牌商都显示 不分推荐
     *
     */


    /**
     * 推荐设计师
     */
    public function recommendDesigner(Request $request)
    {
        $per_page = $request->input('per_page');
        if (strlen($per_page) == 0) {
            $per_page = 8;
        }
        $product_num = $request->input('product_num');
        if (empty($product_num)) {
            $product_num = 10;
        }
        $sort = $request->get('sort');//0降序  1升序
        if (strlen($sort) == 0) {
            $sort = 0;
        }
        $user_id = $request->input('user_id');
        $sort_type = $request->get('sort_type');//1最新 2人气 3销量
        if (strlen($sort_type) == 0) {
            $sort_type = 1;
        }
        if ($sort_type == 1) {
            $result = $this->commendDesignerByLatest($per_page, $user_id, $sort, $product_num);
            return $result;
        } else if ($sort_type == 2) {
            $result = $this->commendDesignerByFansNum($per_page, $user_id, $sort, $product_num);
            return $result;
        } else if ($sort_type == 3) {
            $result = $this->commendDesignerBySellNum($per_page, $user_id, $sort, $product_num);
            return $result;
        } else {
            $arr = array('message' => '参数错误', 'result' => (object)array(), 'state' => 'M09001');
            return $arr;
        }
    }

    /**
     * 推荐品牌
     */
    public function recommendBrand(Request $request)
    {
        $per_page = $request->get('per_page');
        if (strlen($per_page) == 0) {
            $per_page = 8;
        }
        $product_num = $request->input('product_num');
        if(empty($product_num)){
            $product_num=10;
        }
        $sort = $request->get('sort');//0 降序  1反序
        if (strlen($sort) == 0) {
            $sort = 0;
        }
        $user_id = $request->get('user_id');
        $sort_type = $request->get('sort_type');//1最新 2人气 3销量
        if (strlen($sort_type) == 0) {
            $sort_type = 1;
        }
        if ($sort_type == 1) {
            $result = $this->commendBrandByLatest($per_page, $user_id, $sort, $product_num);
            return $result;
        } else if ($sort_type == 2) {
            $result = $this->commendBrandByFansNum($per_page, $user_id, $sort, $product_num);
            return $result;
        } else if ($sort_type == 3) {
            $result = $this->commendBrandBySellNum($per_page, $user_id, $sort, $product_num);
            return $result;
        } else {
            $arr = array('message' => '参数错误', 'result' => (object)array(), 'state' => 'M09001');
            return $arr;
        }
    }


    /**
     * 设计师推荐(最新) sort_type=1
     */
    public function commendDesignerByLatest($per_page, $user_id, $sort, $product_num)
    {
        if ($sort == 0) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['state' => 1])
                ->select('user_id', 'company')
                ->orderBy('pass_time', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['state' => 1])
                ->select('user_id', 'company')
                ->orderBy('pass_time', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }
        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 1);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '设计师推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }

    /**
     * 设计师推荐(销量) sort_type=3
     */
    public function commendDesignerBySellNum($per_page, $user_id, $sort, $product_num)
    {
        if ($sort == 0) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['state' => 1])
                ->select('user_id', 'company')
                ->orderBy('sales_num', 'desc')
                ->orderBy('designer_authentication_id', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['is_commend' => 1, 'state' => 1])
                ->select('user_id', 'company')
                ->orderBy('sales_num', 'asc')
                ->orderBy('designer_authentication_id', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }
        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 1);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '设计师推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }

    /**
     * 设计师推荐(人气) sort_type=2
     */
    public function commendDesignerByFansNum($per_page, $user_id, $sort, $product_num)
    {
        if ($sort == 0) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['state' => 1])
                ->select('user_id', 'company')
                ->orderBy('fans_num', 'desc')
                ->orderBy('designer_authentication_id', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['is_commend' => 1, 'state' => 1])
                ->select('user_id', 'company')
                ->orderBy('fans_num', 'asc')
                ->orderBy('designer_authentication_id', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }

        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 1);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '设计师推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }


    /**
     * 品牌推荐(最新) sort_type=1
     */
    public function commendBrandByLatest($per_page, $user_id, $sort, $product_num)
    {
        if ($sort == 0) {
            $CommendLists = DB::table('brand_authentications')
                ->where(['state' => 1])
                ->select('user_id')
                ->orderBy('pass_time', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['is_commend' => 1, 'state' => 1])
                ->select('user_id')
                ->orderBy('pass_time', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }

        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 2);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '品牌推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }

    /**
     * 品牌推荐(作品) sort_type=3
     */
    public function commendBrandBySellNum($per_page, $user_id, $sort, $product_num)
    {

        if ($sort == 0) {
            $CommendLists = DB::table('brand_authentications')
                ->where(['state' => 1])
                ->select('user_id')
                ->orderBy('sales_num', 'desc')
                ->orderBy('brand_authentication_id', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('brand_authentications')
                ->where(['is_commend' => 1, 'state' => 1])
                ->select('user_id')
                ->orderBy('sales_num', 'asc')
                ->orderBy('brand_authentication_id', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }


        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 2);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '品牌推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }

    /**
     * 品牌推荐(人气) sort_type=3
     */
    public function commendBrandByFansNum($per_page, $user_id, $sort, $product_num)
    {
        if ($sort == 0) {
            $CommendLists = DB::table('brand_authentications')
                ->where(['state'=>1])
                ->select('user_id')
                ->orderBy('fans_num', 'desc')
                ->orderBy('brand_authentication_id', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('designer_authentications')
                ->where([ 'state' => 1])
                ->select('user_id')
                ->orderBy('fans_num', 'asc')
                ->orderBy('brand_authentication_id', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }

        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 2);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '品牌推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }

    /**
     * 品牌推荐(字母) sort_type=4
     */
    public function commendBrandByBrandName($per_page, $user_id, $sort, $product_num)
    {
        if ($sort == 0) {
            $CommendLists = DB::table('brand_authentications')
                ->where(['state' => 1])
                ->select('user_id')
                ->orderBy('company_name', 'desc')
                ->orderBy('brand_authentication_id', 'desc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        } else if ($sort == 1) {
            $CommendLists = DB::table('designer_authentications')
                ->where(['is_commend' => 1, 'state' => 1])
                ->select('user_id')
                ->orderBy('company_name', 'asc')
                ->orderBy('brand_authentication_id', 'asc')
                ->distinct()
                ->paginate($per_page)
                ->toArray();
        }

        $data = $CommendLists['data'];
        if ($data == null) {
            $arr = array('message' => '暂无推荐', 'result' => (object)array(), 'state' => 'M00000');
            return $arr;
        }
        $commend_arr = [];
        foreach ($data as $recom) {
            $commend_user_id = $recom->user_id;
            $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
            $nick_name = $this->UserTool->nicknameByid($commend_user_id);
            $avatar = $this->UserTool->headerimgByid($commend_user_id);
            $products = $this->Product->getProductsByUserId($commend_user_id, $product_num);
            $cover_map = $this->UserTool->coverMap($commend_user_id);
            $job = $this->UserTool->getjob($commend_user_id, 2);
            $fans_num = $this->UserTool->fansNum($commend_user_id);
            $recom->fans_num = $fans_num;
            $recom->job = $job;
            $recom->cover_map = $cover_map;
            $recom->nick_name = $nick_name;
            $recom->avatar = $avatar;
            $recom->is_follow = $is_follow;
            $recom->products = $products['data'];
            $commend_arr[] = $recom;
        }
        $arr = array('per_page' => $CommendLists['per_page'],
            'current_page' => $CommendLists['current_page'],
            'total_page' => $CommendLists['last_page'],
            'data' => $commend_arr);
        $result = array('message' => '品牌推荐', 'result' => $arr, 'state' => 'M00000');
        return $result;
    }


    /**
     * 发现页推荐设计师
     */
    public function commendDesignerToIdex($user_id, $product_num, $user_ids)
    {
        $commend_arr = [];
        foreach ($user_ids as $item) {
            $commend_user_id = $item->resource_id;
            $hasuser = DB::table('users')
                ->where('user_id', $commend_user_id)
                ->where('user_type','<>',0)
                ->first();
            $data = (object)array();
            if ($hasuser != null) {
                $data->user_id = $commend_user_id;
                $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
                $nick_name = $this->UserTool->nicknameByid($commend_user_id);
                $avatar = $this->UserTool->headerimgByid($commend_user_id);
                $products = $this->Product->getProductsByUserId($commend_user_id, $product_num, $user_id);
                $job = $this->UserTool->getjob($commend_user_id, 1);
                $company = $this->UserTool->getcompany($commend_user_id);
                $cover_map = $this->UserTool->coverMap($commend_user_id);
                $data->cover_map = $cover_map;
                $data->nick_name = $nick_name;
                $data->avatar = $avatar;
                $data->job = $job;
                $data->company = $company;
                $data->is_follow = $is_follow;
                $data->products = $products['data'];
                $commend_arr[] = $data;
            }
        }
        return $commend_arr;
    }


    /**
     * 发现页推荐品牌
     */
    public function commendBrandToIndex($user_id, $product_num, $user_ids)
    {
        $commend_arr = [];
        foreach ($user_ids as $item) {
            $commend_user_id = $item->resource_id;
            $hasuser = DB::table('users')
                ->where('user_id', $commend_user_id)
                ->where('user_type','<>',0)
                ->first();
            $data = (object)array();
            if ($hasuser != null) {
                $data->user_id = $commend_user_id;
                $logo= DB::table('brand_authentications')
                    ->where(['user_id' => $commend_user_id])
                    ->value('logo');
                $is_follow = $this->UserTool->is_follow($user_id, $commend_user_id);
                $nick_name = $this->UserTool->nicknameByid($commend_user_id);
                $products = $this->Product->getProductsByUserId($commend_user_id, $product_num, $user_id);
                $job = $this->UserTool->getjob($commend_user_id, 2);
                $cover_map = $this->UserTool->coverMap($commend_user_id);
                $data->cover_map = $cover_map;
                $data->nick_name = $nick_name;
                $data->avatar = asset('images/'.$logo);
                $data->job = $job;
                $data->is_follow = $is_follow;
                $data->products = $products['data'];
                $commend_arr[] = $data;
            }
        }
        return $commend_arr;
    }
}
